Ext.define('productSel', {
	extend : 'Ext.window.Window',
	title : '选择产品',
	collapsible : true,
	width : 660,
	height : 450,
	autoScroll : false,
	OrgGrid : null,
	queryForm : null,
	codeText : null,
	codeTree : null,
	codeValue : null,
	initComponent : function() {
		this.OrgGrid = this.getGrid();
		this.queryForm = this.getForm();
		this.codeTree = this.getCodeTree();
		// 调用父类方法
		this.layout = 'border';
		this.items = [{
					layout : 'fit',
					region : 'west',
					width : 200,
					items : [this.codeTree]
				}, {
					layout : 'border',
					region : 'center',
					border : false,
					items : [{
								layout : 'fit',
					region : 'north',
					border : false,
								items : [this.queryForm]
							}, {layout : 'fit',
					region : 'center',
					border : false,
								items : [this.OrgGrid]
							}]
				}];
		this.buttons = [ {
					text : '选择',
					scope : this,
					handler : this.selAccount
				}, {
					text : '取消',
					scope : this,
					handler : this.onWinHide
				}];
		this.closeAction = 'hide'; // 窗口关闭的方式：hide/close
		this.maximizable = false; // 是否可以最大化
		this.minimizable = false; // 是否可以最小化
		this.closable = false; // 是否可以关闭
		this.modal = true; // 是否为模态窗口
		this.resizable = false; // 是否可以改变窗口大小

		this.addEvents({
					"onSelectItem" : true
				});// 加入自定义事件
		this.callParent(arguments);

	},
	getForm : function() {
		if (this.queryForm) {
			return this.queryForm;
		} else {

			var queryPanel = new Ext.Panel({
						frame : true,
						labelWidth : 60,
						labelAlign : "right",
						layout : "column",
						name : "QUERYPANEL",
						items : [{
									xtype : "textfield",
									labelWidth : 30,
									name : "productName",
									withd:120,
									fieldLabel : '名称'
								}, {
									xtype : "textfield",
									fieldLabel : '编号 ',
									withd:120,
									labelWidth : 30,
									name : "productNumber"
								}, {
									xtype : 'button',
									text : '查询',
									scope : this,
									 handler: function () {
                                         this.OrgGrid.store.load();
                                   }
								}]
					});
			return queryPanel;
		}
	},
	getGrid : function() {
		if (this.OrgGrid) {
			return this.OrgGrid;
		} else {
			var gridAccount = new gridView({
						gridId : "query-account",
						autoSelectFirstRow : false,
						checkboxSelectionModel : false,
                        entityClassName: 'crm.rover.inventory.entity.Product',
                        queryUrl: 'productController/query.action',
						recordPk : ["id"],
						tbar : [{
									xtype : 'label',
									text : '当前选择分类：'
								}, {
									xtype : 'textfield',
									id : "txtDeptname",
									readOnly:true
								}]
					});

			gridAccount.getStore().on('beforeload', function(store, options) {
                store.proxy.extraParams.name =this.getForm().getForm().findField("productName").getValue();
                store.proxy.extraParams.productNumber =this.getForm().getForm().findField("productNumber").getValue();
				store.proxy.extraParams.category = this.codeValue;
					}, this, true);
			return gridAccount;
		}
	},
	getCodeTree : function() {
		if (this.codeTree) {
			return this.codeTree;
		} else {
			var tree =new Ext.tree.Panel({
                layout : 'fit',
                region : 'east',
                collapsible : true,
                collapsedTitle : true,
                border : true,
                split : false,
                width : 200,
                animate : false,
                autoScroll : true,
                enableDD : false,
                containerScroll : true,
                lines : true,
                rootVisible : true,
                title : '分类',
                store:Ext.create('Ext.data.TreeStore', {
                    proxy : {
                        type : 'ajax',
                        url : getRootPath()+'/codeValueController/treeJsonId.action?idType=treeCode&spareProperty=productCategories'
                    },
                    sorters : [{
                        property : 'leaf',
                        direction : 'ASC'
                    }, {
                        property : 'text',
                        direction : 'ASC'
                    }],
                    root : {
                        id : "",
                        text : "所有分类",
                        expanded : true
                    }
                })
            });
			if (Ext.isIE) {
				tree.animate = false;
				tree.animCollapse = false;
			}
			var me = this;
			tree.on({
						scope : this,
						'itemclick' : function(thisTree, record, item, index,
								e, options) {
							if (null != record.get('id')
									&& "root-0" != record.get('id')) {
								var id = record.get('id');
								me.codeValue = id;
								me.codeText = record.get('text');

							} else if ("root-0" == record.get('id')) {
								me.codeText = '所有分类';
								me.codeValue = '0';

							} else {
								me.codeValue = "";

							}
							me.OrgGrid.getStore().load();
							Ext.getCmp("txtDeptname")
									.setValue(me.codeText);
						}
					});
			return tree;
		}
	},
	onWinHide : function() {
		this.hide();
	},
	selAccount : function() {
		var record = this.OrgGrid.getSelectionModel().getSelection();
		if (record) {
	
			this.fireEvent('onSelectItem', record[0].data.id, record[0].data.name);// 激发自定义事件
			this.hide();
		} else {
			Ext.Msg.alert("提示", "请选择对应的老师！");
		}
	}
});